/*
 * EntityOT.java
 *
 * $Id: EntityOT.java,v 1.4 2009-09-29 11:34:44 mario Exp $
 */
package org.ceteca.explica.core.installer;

import java.util.ArrayList;

/**
 * ValueObject with the data of an Entity item.
 * <br/>
 * Responsibilities:
 * <ul>
 *  <li>Store the entity data.</li>
 * </ul>
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2008 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class EntityOT {
	/** Entity key. It's the entity key for indexing and accessing the entity data.
	 */
	private String key;
	/** Entity name. It's the label of the access button of the entity
	 * that will be showed to the user.
	 */
	private String name;
	/** Entity icon. It's the icon of the access button of the entity
	 * that will be showed to the user.
	 */
	private String icon;
	/** Entity id field. It's the name of the identifier field of the entity.
	 */
	private String idField;
	/** Entity helper classname. The helper class will be in charge of
	 * the events and actions associated to the entity. It must implement
	 * the EntityLauncher interface.
	 * @see org.ceteca.explica.core.EntityLauncher.
	 */
	private String entityHelperClass;
	/** Entity visibility. It will be true when the entity button is enabled
	 * in the module toolbar, or false when it won't be shown.
	 */
	private boolean visible;
	
	/** Entity list of fields. This list include all the fields that
	 * will be showed in the preview table. It also includes the fields
	 * that will be enabled to query the entity by.
	 */
	private ArrayList<FieldOT> lFields;
	
	/**
	 * EntityOT constructor. It initializes all the entity values.
	 * @param key String, entity key.
	 * @param name String, entity name.
	 * @param icon String, entity icon.
	 * @param idField String, entity's identifier field name.
	 * @param entityHelperClass String, entity helper classname.
	 */
	public EntityOT(String key, String name, String icon, String idField, String entityHelperClass) {
		this.setKey(key);
		this.setName(name);
		this.setIcon(icon);
		this.setIdField(idField);
		this.setEntityHelperClass(entityHelperClass);
		this.setVisible(true);
	}
	
	/**
	 * EntityOT constructor. It initializes all the entity values.
	 * @param key String, entity key.
	 * @param name String, entity name.
	 * @param icon String, entity icon.
	 * @param idField String, entity's identifier field name.
	 * @param entityHelperClass String, entity helper classname.
	 * @param visible String, entity visibility.
	 */
	public EntityOT(String key, String name, String icon, String idField, String entityHelperClass, String visible) {
		this.setKey(key);
		this.setName(name);
		this.setIcon(icon);
		this.setIdField(idField);
		this.setEntityHelperClass(entityHelperClass);
		this.setVisible(Boolean.parseBoolean(visible));
	}

	/**
	 * Gets the entity key.
	 * @return String, entity key.
	 */
	public String getKey() {
		return key;
	}
	/**
	 * Sets up the entity key.
	 * @param key String, entity key.
	 */
	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * Gets the entity helper classname.
	 * @return String, entity helper classname.
	 */	
	public String getEntityHelperClass() {
		return entityHelperClass;
	}
	/**
	 * Sets up the entity helper classname.
	 * @param entityHelperClass String, entity helper classname.
	 */	
	public void setEntityHelperClass(String entityHelperClass) {
		this.entityHelperClass = entityHelperClass;
	}

	/**
	 * Gets the entity icon.
	 * @return String, entity icon.
	 */	
	public String getIcon() {
		return icon;
	}
	/**
	 * Sets up the entity icon.
	 * @param icon String, entity icon.
	 */	
	public void setIcon(String icon) {
		this.icon = icon;
	}

	/**
	 * Gets the entity name.
	 * @return String, entity name.
	 */	
	public String getName() {
		return name;
	}
	/**
	 * Sets up the entity name.
	 * @param name String, entity name.
	 */	
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Gets the entity visibility
	 * @return boolean, True if the field is visible; False otherwise.
	 */
	public boolean isVisible() {
		return visible;
	}
	/**
	 * Sets up the entity visibility
	 * @param visible boolean, True if the field is visible; False otherwise.
	 */
	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	/**
	 * Gets the entity id field.
	 * @return String, entity's identifier field name.
	 */	
	public String getIdField() {
		return idField;
	}
	/**
	 * Sets up the entity id field.
	 * @param name String, entity's identifier field name.
	 */	
	public void setIdField(String idField) {
		this.idField = idField;
	}
	
	/**
	 * Gets the entity fields' list.
	 * @return ArrayList<FieldOT>, entity list of fields.
	 */
	public ArrayList<FieldOT> getLFields() {
		return lFields;
	}
	/**
	 * Sets up the entity fields' list.
	 * @param lFields ArrayList<FieldOT>, entity list of fields.
	 */
	public void setLFields(ArrayList<FieldOT> fields) {
		lFields = fields;
	}
	/**
	 * Adds a new field to the entity fields' list.
	 * @param field FieldOT, data of the entity field.
	 */
	public void addField(FieldOT field) {
		if (lFields == null)
			lFields = new ArrayList<FieldOT>();
		
		lFields.add(field);
	}
	
	/** 
	 * Returns the field of the entity.
	 * @param fieldId String, key of the selected field.
	 * @return FieldOT, field of the entity.
	 */
	public FieldOT getField(String fieldId) {
		if (fieldId != null && !fieldId.isEmpty() && this.lFields != null && this.lFields.size() > 0) {
			FieldOT field = null;
			for (int i=0; i<this.lFields.size() && field == null; i++) {
				if (this.lFields.get(i).getId().equals(fieldId))
					field = this.lFields.get(i);
			} // for each entity field
			
			return field;
		}
		else
			return null;
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: EntityOT.java,v 1.4 2009-09-29 11:34:44 mario Exp $";
	}
}
